package org.jeecg.modules.app.v1.home.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.config.aspct.ApiCheckDataAuth;
import org.jeecg.modules.app.v1.home.entity.ApiEmptyDeviceEntity;
import org.jeecg.modules.app.v1.home.entity.ApiEmptyDeviceEntityExtends;
import org.jeecg.modules.app.v1.home.entity.ApiHomeDeviceEntity;
import org.jeecg.modules.app.v1.home.entity.DeviceParam;
import org.jeecg.modules.aspect.ApiLoginAspct;
import org.jeecg.modules.power.device.service.IYlDeviceService;
import org.jeecg.modules.power.group.entity.GroupUser;
import org.jeecg.modules.power.records.service.IYlDeviceStatusRecordsService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 首页设备统计
 *
 * @author scw
 * @version v1.0
 * @date 2024-08-01
 */
@Api(tags="APP管理端_首页设备统计")
@RestController
@RequestMapping("/api/v1/device")
@Slf4j
@RequiredArgsConstructor
public class ApiHomeController {

    private final IYlDeviceStatusRecordsService deviceStatusRecordsService;

    @ApiOperation(value="1_首页设备统计", notes="首页设备统计")
    @GetMapping(value = "/countDevice")
    @ApiImplicitParams({
        @ApiImplicitParam(name="categoryId",value = "设备分类ID"),
        @ApiImplicitParam(name="flag",value = "1小组2BD"),
        @ApiImplicitParam(name="groupId",value = "flag=1时小组Id，flag=2时bdId"),
    })
    @ApiOperationSupport(order = 1)
    @ApiLogin
    public Result<ApiHomeDeviceEntity> countDevice(String categoryId, String flag, String groupId, HttpServletRequest request){
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        ApiHomeDeviceEntity entity = deviceStatusRecordsService.countDeviceForHome(categoryId, "2", bd.getId());
        return Result.OK(entity);
    }

    @ApiLogin
    @ApiOperation(value="2_空仓、满仓、离线设备统计", notes="空仓、满仓、离线设备统计")
    @PostMapping(value = "/countEmptyDevice")
    @ApiOperationSupport(order = 2)
//    @ApiCheckDataAuth
    public Result<ApiEmptyDeviceEntity> shopEmptyDevice(
//            @RequestBody DeviceParam param,
            String flag,String groupId,String type,String orderBy,String categoryId,Integer min,Integer max,
                                                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        DeviceParam param = new DeviceParam();
        param.setType(type);
        param.setMax(max);
        param.setMin(min);
        param.setCategoryId(categoryId);
        param.setOrderBy(orderBy);

        ApiEmptyDeviceEntity entity = deviceStatusRecordsService.countEmptyDevice(param);
        Page<ApiEmptyDeviceEntityExtends> page = new Page<>(pageNo,pageSize);
        param.setFlag(flag);
        param.setGroupId(groupId);
        List<ApiEmptyDeviceEntityExtends> list = deviceStatusRecordsService.shopDevice(page,param);
        entity.setList(list);
        return Result.OK(entity);
    }


}
